<template>
  <div>
    <!-- 头部 -->
    <el-card>
      <i class="el-icon-s-order" style="font-size: 18px; margin-right: 6px"></i>
      <span>商品分类列表</span>
      <el-button @click="$router.back()" size="mini" class="tian"
        >返回</el-button
      >
    </el-card>

    <!-- 表单 -->
    <el-form ref="formAdd" :model="form" label-width="150px" :rules="rules">
      <el-form-item label="分类名称:">
        <el-input v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item label="上级分类:">
        <el-select v-model="form.parent_id" placeholder="请选择上级分类">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in selectProductCateList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="数量单位:">
        <el-input v-model="form.product_unit"></el-input>
      </el-form-item>

      <el-form-item label="排序:">
        <el-input v-model="form.sort"></el-input>
      </el-form-item>

      <el-form-item label="分类是否启用:">
        <el-radio-group v-model="form.nav_status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="移动端是否显示:">
        <el-radio-group v-model="form.show_status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="分类图标:">
        <el-upload
          class="upload-demo"
          action="/manager/api/auth/category/upload_category"
          :on-remove="handleRemove"
          :on-success="handleSuccess"
          :file-list="fileList"
          :successCallBack="getCategoryIcon"
          list-type="picture"
          :multiple="false"
          :limit="1"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="关键字:">
        <el-input v-model="form.keywords"></el-input>
      </el-form-item>

      <el-form-item label="描述:">
        <el-input v-model="form.description"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="tijiao">提交</el-button>
        <el-button type="danger" @click="chongzhi">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  getCategory,
  getCategoryId,
  updateCategory,
} from "../../../../api/categoryApi";
let defaultProductCate = {
  name: "",
  parent_id: "",
  product_unit: "",
  sort: 0,
  nav_status: 0,
  show_status: 0,
  icon: "", // 分类图标
  keywords: "",
  description: "",
};
export default {
  data() {
    return {
      form: Object.assign({}, defaultProductCate),
      fileList: [],
      selectProductCateList: [],
      rules: {
        name: [
          { require: true, message: "请输入分类的名称", trigger: "blur" },
          { min: 3, max: 20, message: "长度在 3 到 20个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.huoqu();

    getCategoryId(this.$route.query.id).then((res)=>{
        if(res.status == 1){
            console.log(res);
            this.form = res.data[0]
        }
    })
  },
  watch: {
    icon(value) {
      console.log(value)
      this.fileList.push({
        name: value.substr(value.lastIndexOf("/") + 1),
        url: "http://localhost:5000" + value,
      });
    },
  },
  methods: {
    //获取一级分类名称
    huoqu() {
      getCategory(0, 1, 100).then((res) => {
        if (res.status == 1) {
          this.selectProductCateList = res.data.category_list;
          this.selectProductCateList.unshift({ id: 0, name: "无上级分类" });
        }
      });
    },


    //上传图片
    handleSuccess(response) {
      // 当图片上传成功后，调用此访问
      // console.log(response,file, fileList);
      if (response.status === 1) {
        this.getCategoryIcon(response.data);
      }
    },
    handleRemove(file, fileList) {
      // 当移除组件时调用
      if (file.status === "success") {
        this.getCategoryIcon(null);
      }
    },
     getCategoryIcon(data) {
      this.form.icon = data.name;
    },



    //提交
    tijiao() {
      this.$refs.formAdd.validate(async (valid) => {
        console.log(valid);
        if (valid) {
          addCategory(this.form).then((res) => {
            console.log(res);
            if (res.status == 1) {
              this.$message.success(res.msg);
              this.chongzhi(); // 清空表单数据
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("分类名称不能为空~");
          return false;
        }
      });
    },
    //重置
    chongzhi() {
      this.$refs.formAdd.resetFields();
      (this.form = Object.assign({}, defaultProductCate)), this.huoqu();
    },
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin: 20px 0;
}
.tian {
  float: right;
}
</style>